package cn.stylefeng.guns.sys.modular.system.controller;


import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.db2.service.BusinessService;
import cn.stylefeng.guns.sys.modular.system.model.params.AreaParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProDataViewParam;
import cn.stylefeng.guns.sys.modular.system.model.result.*;
import cn.stylefeng.guns.sys.modular.system.service.ProjectDataViewService;
import cn.stylefeng.guns.sys.modular.system.service.ProjectService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/gcDataView")
public class ProjectDataViewController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ProjectDataViewController.class);
    @Autowired
    private ProjectDataViewService projectDataViewService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private ProjectService projectService;

    /**
     * 国
     */
    @RequestMapping("/findCountrys")
    @ResponseBody
    public ResponseData findCountrys() {

        AreaParam param = new AreaParam();
        param.setPid(0L);
        List<AreaResult> areaList = projectDataViewService.getAreasByParam(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, areaList);
    }

    /**
     * 根据父id获取下级区域
     */
    @RequestMapping("/findNextAreas")
    @ResponseBody
    public ResponseData findNextAreas(AreaParam param) {
        logger.info("工程数据预览参数-根据父id获取下级区域：{}", param.toString());
        if (param.getPid() == null) {
            return ResponseData.error(412, "请求失败，pid为必传参数");
        }

        List<AreaResult> areaList = projectDataViewService.getAreasByParam(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, areaList);
    }

    /**
     * 门店状态
     */
    @RequestMapping("/getDataOfShopStatus")
    @ResponseBody
    public ResponseData getDataOfShopStatus(ProDataViewParam param) {
        logger.info("工程数据预览参数-门店状态：{}", param.toString());
        if (param.getYear() == null) {
            return ResponseData.error(412, "请求失败，年份为必传参数");
        }

        List<DataResult> dataList = projectDataViewService.getDataOfShopStatus(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, dataList);
    }

    /**
     * 确认开业及预计开业数据
     */
    @RequestMapping("/getDataOfOpen")
    @ResponseBody
    public ResponseData getDataOfOpen(ProDataViewParam param) {
        logger.info("工程数据预览参数-确认开业及预计开业数据：{}", param.toString());
        if (param.getYear() == null) {
            return ResponseData.error(412, "请求失败，年份为必传参数");
        }

        List<OpenDataInfoResult> dataList = projectDataViewService.getDataOfOpen(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, dataList);
    }


    /**
     * 门店区域分布列表（地图左下角）
     */
    @RequestMapping("/getDataOfShopArea")
    @ResponseBody
    public ResponseData getDataOfShopArea() {

        ShopAreaResult data = projectDataViewService.getDataOfShopArea();
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 门店区域分布
     */
    @RequestMapping("/getValGroupByProvince")
    @ResponseBody
    public ResponseData getValGroupByProvince() {
        List<ShopAreaDataResult> data = projectDataViewService.getValGroupByProvince();
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }


    /**
     * 地图-点击省显示开业及未开业详情数量
     */
    @RequestMapping("/getOpenDaraByProvince")
    @ResponseBody
    public ResponseData getOpenDaraByProvince(ProDataViewParam param) {
        logger.info("工程数据预览参数-点击省显示开业及未开业详情数量：{}", param.toString());
        //省id及省名称必须传入一个
        if (param.getProvinceId() == null && StringUtils.isBlank(param.getProvinceName())) {
            return ResponseData.error(412, "请求失败，省份id或省份名称至少传入一个");
        }

        ProvinceDataResult data = projectDataViewService.getOpenDaraByProvince(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 门店楼层分布-楼层数据
     */
    @RequestMapping("/getFloorData")
    @ResponseBody
    public ResponseData getFloorData() {
        List<DataResult> data = projectDataViewService.getFloorData();
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 门店租期
     */
    @RequestMapping("/getShopLeaseData")
    @ResponseBody
    public ResponseData getShopLeaseData() {
        List<DataResult> data = projectDataViewService.getShopLeaseData();
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 门店免租期
     */
    @RequestMapping("/getFreeLeaseData")
    @ResponseBody
    public ResponseData getFreeLeaseData() {
        List<DataResult> data = projectDataViewService.getFreeLeaseData();
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 获取门店宿舍租金
     */
    @RequestMapping("/getDormitoryRentData")
    @ResponseBody
    public ResponseData getDormitoryRentData(ProDataViewParam param) {
        logger.info("工程数据预览参数-获取门店宿舍租金：{}", param.toString());
        if (param.getParam() == null || param.getParam() == 0 || param.getParam() < 0) {
            return ResponseData.error(412, "请求失败，区间长度为必传参数且必须大于0");
        }

        List<DataResult> data = projectDataViewService.getDormitoryRentData(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 门店租金数据
     */
    @RequestMapping("/getShopRentData")
    @ResponseBody
    public ResponseData getShopRentData(ProDataViewParam param) {
        logger.info("工程数据预览参数-获取门店租期数据：{}", param.toString());


        List<DataResult> data = projectDataViewService.getShopRentData(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 根据门店租金数据区间获取对应区间的门店
     *
     * @param param
     * @return
     */
    @RequestMapping("/getShopRentDataByRangeKey")
    @ResponseBody
    public ResponseData getShopRentDataByRangeKey(ProDataViewParam param) {
        logger.info("工程数据预览参数-获取门店租期数据：{}", param.toString());
        if (StringUtils.isBlank(param.getStart())) {
            return ResponseData.error(412, "请求失败，区间开始参数不能为空");
        }
        if (StringUtils.isBlank(param.getEnd())) {
            return ResponseData.error(412, "请求失败，区间借宿参数不能为空");
        }

        param.setKey("[" + param.getStart() + "," + param.getEnd() + ")");

        List<Map<String, Object>> data = projectDataViewService.getShopRentDataByRangeKey(param);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }

    /**
     * 大米消耗
     *
     * @param param
     * @return
     */
    @RequestMapping("/getShopRiceConsumption")
    @ResponseBody
    public ResponseData getShopRiceConsumption(ProDataViewParam param) {
        logger.info("工程数据预览参数-大米消耗：{}", param.toString());
        if (StringUtils.isBlank(param.getStartMonthDate())) {
            return ResponseData.error(412, "请求失败，开始月时间参数不能为空");
        }
        if (StringUtils.isBlank(param.getEndMonthDate())) {
            return ResponseData.error(412, "请求失败，结束月时间参数不能为空");
        }
        if (param.getType() == null) {
            return ResponseData.error(412, "请求失败，排序参数不能为空");
        }
        if (param.getShowZero()==null) {
            return ResponseData.error(412, "请求失败，是否显示非零数据参数不能为空");
        }

        //月份参数处理
        String fisrtDayOfMonth = DateUtil.getFisrtDayOfMonth(param.getStartMonthDate());
        String endDayOfMonth = DateUtil.getLastDayByMonth(param.getEndMonthDate());

        //TODO-1.就餐人数
        ProDataViewParam param2 = new ProDataViewParam();
//        param2.setStartTime(fisrtDayOfMonth.trim() + " 04:00:00");
//        param2.setEndTime(DateUtil.get4OclockByDay(endDayOfMonth.trim()));
        param2.setStartMonthDate(param.getStartMonthDate());
        param2.setEndMonthDate(param.getEndMonthDate());
        List<Map<String, Object>> peopleQtyDataMapList = projectDataViewService.findPeopleQtyByParam(param2);
        //TODO-1.2.外卖主食专属
        ProDataViewParam param3=new ProDataViewParam();
        param3.setStartMonthDate(param.getStartMonthDate());
        param3.setEndMonthDate(param.getEndMonthDate());
        List<Map<String, Object>> riceDataMapList = projectDataViewService.findTakeAwayRiceDataByParam(param3);
        //TODO-2.获取大米消耗并计算消耗比 消耗比=（大米盘亏数据-外卖主食专属）/就餐人数
        param.setStartTime(fisrtDayOfMonth + " 00:00:00");
        param.setEndTime(endDayOfMonth + " 23:59:59");
        List<Map<String, Object>> data = projectDataViewService.getShopRiceConsumption(param, peopleQtyDataMapList,riceDataMapList);
        return ResponseData.success(200, ResponseData.DEFAULT_SUCCESS_MESSAGE, data);
    }
}
